﻿Imports EstoqueDAL
Imports EstoqueBLL
Public Class PecaUC
    Dim objBLL As New PECABLL
#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("pec_codigo") Is Nothing) Then
                    objBLL = New PECABLL(Val(DGVDados.Rows(linha.Index).Cells("pec_codigo").Value.ToString()))
                End If
            Next
            Dim cad As New PecaCAD
            If (objBLL.pec_codigo > 0) Then
                cad.lcodigo.Text = objBLL.pec_codigo
                cad.lcodigo.Visible = False
                cad.LTitulo.Text = objBLL.pro_descricao
                cad.Label1.Visible = True
                TSBAlterar.BackColor = Color.Gainsboro
                cad.ShowDialog()
                CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        Try
            Dim cad As New PecaCAD
            TSBCadastrar.BackColor = Color.Gainsboro
            cad.ShowDialog()
            CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            TSBCadastrar.BackColor = Color.Transparent
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBCadastrar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Carrega Grid"
    Public Sub carregagrid()
        Dim dt As New DataTable
        dt = objBLL.localizar(Nothing, 90)
        DGVDados.DataSource = dt
        DGVDados.Refresh()

    End Sub
#End Region

#Region "Click da Grid"
    Private Sub DGVDados_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellClick
        objBLL = Nothing
        If (e.RowIndex >= 0) Then
            objBLL = New PECABLL(Val(DGVDados.Rows(e.RowIndex).Cells(0).Value))
        End If
    End Sub
#End Region

#Region "Combobox Filtro"
    Private Sub CBFiltro_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        Select Case CBFiltro.Text
            Case "Código de Barras"
                TXTLocalizar.Clear()
                TXTLocalizar.Visible = True
                CBStatus.Visible = False
                CBDisponibilidade.Visible = False
            Case "Código Original"
                TXTLocalizar.Clear()
                TXTLocalizar.Visible = True
                CBStatus.Visible = False
                CBDisponibilidade.Visible = False
            Case "Código do Fabricante"
                TXTLocalizar.Clear()
                TXTLocalizar.Visible = True
                CBStatus.Visible = False
                CBDisponibilidade.Visible = False
            Case "Status"
                TXTLocalizar.Visible = False
                CBStatus.Visible = True
                CBDisponibilidade.Visible = False
                CBStatus.SelectedIndex = 0
                CBStatus_SelectedIndexChanged(Nothing, Nothing)
            Case "Descrição"
                TXTLocalizar.Clear()
                TXTLocalizar.Visible = True
                CBStatus.Visible = False
                CBDisponibilidade.Visible = False
            Case "Disponibilidade"
                TXTLocalizar.Visible = False
                CBStatus.Visible = False
                CBDisponibilidade.Visible = True
                CBDisponibilidade.SelectedIndex = 0
                CBDisponibilidade_SelectedIndexChanged(Nothing, Nothing)
        End Select
        If TXTLocalizar.Text = "" Then
            carregagrid()
        End If
    End Sub
#End Region

#Region "Combobox Status"
    Private Sub CBStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (CBStatus.Text = "Ativo") Then sta = 1
        If (CBStatus.Text = "Inativo") Then sta = 2
        DGVDados.DataSource = objBLL.localizar(sta, 3)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Combobox Disponibilidade"
    Private Sub CBDisponibilidade_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CBDisponibilidade.SelectedIndexChanged
        Dim disponibilidade As Char
        If (CBDisponibilidade.Text = "Disponível") Then disponibilidade = Char.Parse("D")
        If (CBStatus.Text = "Indisponível") Then disponibilidade = Char.Parse("I")
        DGVDados.DataSource = objBLL.localizar(disponibilidade, 4)
    End Sub
#End Region

#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("pec_codigo") Is Nothing) Then
                    objBLL = New PECABLL(Val(DGVDados.Rows(linha.Index).Cells("pec_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.fab_codigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If (MessageBox.Show("Confirma a exclusão da peça: " & objBLL.pro_descricao & "?", "Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                    objBLL.sta_codigo = 3
                    objBLL.inseriralterar(objusuarios.Codigo)
                    CBFiltro_SelectedIndexChanged(Nothing, Nothing)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub PecaUC_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            TSBVoltar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "Localizar"
    Private Sub TSBBuscar_Click(sender As System.Object, e As System.EventArgs) Handles TSBBuscar.Click
        If (PLocalizar.Visible) Then
            PLocalizar.Visible = False
            TSBBuscar.BackColor = Color.Transparent
            carregagrid()
        Else
            PLocalizar.Visible = True
            TSBBuscar.BackColor = Color.Gainsboro
            CBStatus.SelectedValue = 0
            CBFiltro.SelectedValue = 0
        End If
    End Sub
#End Region

#Region "Localizar Textbox"
    Private Sub TXTLocalizar_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        Select Case CBFiltro.Text
            Case "Código de Barras"
                If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
            Case "Código Original"
                If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
            Case "Código do Fabricante"
                If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
        End Select
    End Sub
    Private Sub TXTLocalizar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Descrição"
                DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 2)
            Case "Código de Barras"
                DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 5)
            Case "Código Original"
                DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 6)
            Case "Código do Fabricante"
                DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 7)
        End Select
        If TXTLocalizar.Text.Trim = "" Then
            DGVDados.DataSource = objBLL.localizar(TXTLocalizar.Text, 2)
        End If
    End Sub
#End Region

#Region "Load"


    Private Sub PecaUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        carregagrid()
        CBStatus.SelectedIndex = 0
        CBFiltro.SelectedIndex = 0
        CBFiltro.Focus()
    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statustabestoque = "N"
        Dispose()
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint

        If DGVDados.Rows(e.RowIndex).Cells("pec_qtde").Value <= 0 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Tomato
        ElseIf DGVDados.Rows(e.RowIndex).Cells("pec_qtde").Value < DGVDados.Rows(e.RowIndex).Cells("pec_qtdemin").Value Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Yellow
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com duplo click"
    Private Sub DGVDado_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        TSBAlterar_Click(Nothing, Nothing)
    End Sub
#End Region

#Region "DatagridView Deletar"
    Private Sub DGVDado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DGVDados.KeyDown
        If (e.KeyCode = Keys.Delete) Then
            TSBExcluir_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com enter"
    Private Sub DGVDado_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TSBAlterar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "TSB relatorio"
    Private Sub TSBRelatorio_Click(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("pec_codigo") Is Nothing) Then
                objBLL = New PECABLL(Val(DGVDados.Rows(linha.Index).Cells("pec_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.pec_codigo > 0) Then gerarelatrio("Peça: " & objBLL.pro_descricao, "{vw_EstPeca.pec_codigo}=" & objBLL.pec_codigo)
        End If
    End Sub
#End Region

#Region "Relatorio"
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosFRM
            Dim rpt As New PecaDetalhada

            Try
                rpt.SetDatabaseLogon(usuarios, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer1.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer1.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region
End Class
